
var tableInfo={
	tableName:"",
	dataTypes:[],
	fieldList:[],
	init : function() {
		//alert("获取表信息");
		tableInfo.tableName = getUrlParam("tableName");
		tableInfo.getDataTypes();
    },
	
	getDataTypes:function()
	{
		var params = {
					"msgId":"getDataTypes",
					"to":"tableManage",	
					"msg":{
						}
					};
		//alert(params);
		$.post("/watchDog/database/handleManage.req",JSON.stringify(params),function(result){
			//alert(result);
			result = result.replace(/\n/g," ");
			result = result.replace(/\r/g," ");
			var data = $.parseJSON(result);   
		
			if(!DealErrorInfo(data,'获取数据类型失败：')){	 
				return ;
			}
			
			var msg = data.msg;
			
			var dataTypes = msg.dataTypes;
			if(!dataTypes)
			{
				alert("dataTypes is nullptr");
				return ;
			}
			
			if(dataTypes.length < 1)
			{
				alert("dataTypes size is " + dataTypes.length);
				return ;
			}
			
			tableInfo.dataTypes = dataTypes;
			//alert("dataTypes:" + result);														   
			
			//获取表信息
			tableInfo.getTableInfo();
		}).fail(function(){
			tableInfo.showTips("提示",'连接服务器失败');
		});
	},
	getTableInfo:function(){
		
		//alert("获取 " + tableName + " 表信息");
		var params = {
					"msgId":"getTableInfo",
					"to":"tableManage",	
					"msg":{
						"tableName":tableInfo.tableName
						}
					};
		//alert(params);
		$.post("/watchDog/database/handleManage.req",JSON.stringify(params),function(result){
			//alert(result);
			var data = $.parseJSON(result);   
		
			if(!DealErrorInfo(data,'获取配置信息失败：')){	 
				return ;
			}
			
			var msg = data.msg;
			$("#tableDriver").html(msg.tableDriver);
			$("#tableCreateTime").html(msg.createTime);
			
			var fieldList = msg.fieldList;
			if(!fieldList)
			{
				//alert("fieldList is nullptr");
				return ;
			}
			
			//alert("fieldList:" + result);
			for(var i=0;i<fieldList.length;i++)
			{
				fieldList[i].oldName = fieldList[i].name;
			}
			tableInfo.fieldList = fieldList;
			tableInfo.showFieldList();
			//	tableInfo.showTips("提示",'成功刷新代理信息');														   
		}).fail(function(){
			tableInfo.showTips("提示",'连接服务器失败');
		});
	},
	
	addField:function()
	{
		tableInfo.fieldList.push({"name":"","type":tableInfo.dataTypes[0].deviceId,
									"def":"","canBeNull":true,"desc":"","len":tableInfo.dataTypes[0].len });
		
		tableInfo.showFieldList();
	},

	showFieldList:function()
	{
		var fieldList = tableInfo.fieldList;
		var html = "";
		for(var i=0;i<fieldList.length;i++)
		{
			var canBeNull = "";
			if(fieldList[i].canBeNull)
			{
				canBeNull = "checked='checked'";
			}
			var record = "<tr id='fieldInfo"+i+"'><td>" + (i + 1) + "</td><td><input type='text' value='"+fieldList[i].name+"' id='fieldName"+i+"' onBlur='tableInfo.setFieldName("+i+")'></td>"
			 			+ "<td>"+tableInfo.getDataType(fieldList[i].type,i)+"</td>"
						+ "<td><input type='text' value='"+fieldList[i].len+"' class='fieldLen' id='fieldLen"+i+"' onBlur='tableInfo.setFieldLen("+i+")'></td>"
						+ "<td><input type='text' value='"+fieldList[i].def+"' id='fieldDef"+i+"' onBlur='tableInfo.setDef("+i+")'></td>"
						+ "<td><input type='checkbox' "+canBeNull+" class='fieldCanBeNull' id='fieldCanBeNull"+i+"' onBlur='tableInfo.setCanBeNull("+i+")'></td>"
						+ "<td><input type='text' value='"+fieldList[i].desc+"' id='fieldDesc"+i+"' onBlur='tableInfo.setDesc("+i+")'></td>"
						+ "<td><input type='button' value='删除' id='fieldDelete"+i+"' onClick='tableInfo.deleteField("+i+")'></td>"
						+ "</tr>"
			html += record;
		}
		
		$("#fieldList").html(html);
	},
	
	getDataType:function(dataType,index)
	{
		var sel="<select onChange='tableInfo.dataTypeChange(this,"+index+")' id='dataType"+index+"'>";
		var dataTypes = tableInfo.dataTypes;
		for(var i=0;i<dataTypes.length;i++)
		{
			var selected = "";
			if(dataTypes[i].deviceId == dataType)
			{
				selected = "selected='selected'";
			}
			sel += "<option value='"+dataTypes[i].deviceId+"' "+selected+">"+dataTypes[i].deviceId+"</option>";
		}
		sel += "</select>";
		
		return sel;
	},
	
	dataTypeChange:function(that,index)
	{
		var fieldInfo = tableInfo.fieldList[index];
		var dataTypes = tableInfo.dataTypes;
		var fieldDataType = $("#dataType"+index).val();
		
		for(var i=0;i<dataTypes.length;i++)
		{
			if(dataTypes[i].deviceId == fieldDataType )
			{
				fieldInfo.type = fieldDataType;
				if((fieldInfo.len % dataTypes[i].len) != 0)
				{
					fieldInfo.len = dataTypes[i].len;
				}
				break;
			}
		}
		
		$("#fieldLen"+index).val(fieldInfo.len);
	},
	
	setFieldLen:function(index)
	{
		var fieldInfo = tableInfo.fieldList[index];
		var strLen = $("#fieldLen"+index).val();
		strLen = $.trim(strLen);
		
		var len = parseInt(strLen);
		
		if(isNaN(len))
		{
			alert("请输入数字");
			$("#fieldLen"+index).val(fieldInfo.len);
			return;
		}
		
		if(len == 0)
		{
			alert("字段长度不能为0");
			$("#fieldLen"+index).val(fieldInfo.len);
			return;
		}
		
		
		
		var dataTypes = tableInfo.dataTypes;
		
		for(var i=0;i<dataTypes.length;i++)
		{
			if(dataTypes[i].deviceId == fieldInfo.type )
			{
				if((len % dataTypes[i].len) != 0)
				{
					alert("字段长度必须是对应数据类型的单元长度倍数");
					$("#fieldLen"+index).val(fieldInfo.len);
					return ;
				}
				break;
			}
		}
		
		fieldInfo.len = len;
	},
	
	setFieldName:function(index)
	{
		var fieldInfo = tableInfo.fieldList[index];
		var strLen = $("#fieldName"+index).val();
		strLen = $.trim(strLen);
		
		if(strLen == "")
		{
			alert("请输入字段名");
			$("#fieldName"+index).val(fieldInfo.name);
			return;
		}
		
		fieldInfo.name = strLen;
	},
	
	setDef:function(index)
	{
		var fieldInfo = tableInfo.fieldList[index];
		var str = $("#fieldDef"+index).val();
		str = $.trim(str);
		
		fieldInfo.def = str;
	},
	
	setCanBeNull:function(index)
	{
		var fieldInfo = tableInfo.fieldList[index];
		var val = $("#fieldCanBeNull"+index).val();
				
		fieldInfo.canBeNull = val;
	},
	
	setDesc:function(index)
	{
		var fieldInfo = tableInfo.fieldList[index];
		var str = $("#fieldDesc"+index).val();
		str = $.trim(str);
		
		fieldInfo.desc = str;
	},
	
	deleteField:function(index)
	{
		tableInfo.fieldList.splice(index,1);
		tableInfo.showFieldList();
	},
	
	
	setTableFiels:function()
	{
		fieldList = tableInfo.fieldList;
		for(var i=0;i<fieldList.length;i++)
		{
			if(fieldList[i].name == "")
			{
				alert("第"+(i+1) +"个字段的名称为空");
				return ;
			}
		}
		
		var params = {
					"msgId":"setTableInfo",
					"to":"tableManage",	
					"msg":{
						"tableName":tableInfo.tableName,
						"fieldList":tableInfo.fieldList
						}
					};
		//alert(params);
		$.post("/watchDog/database/handleManage.req",JSON.stringify(params),function(result){
			//alert(result);
			var data = $.parseJSON(result);   
		
			if(!DealErrorInfo(data,'设置表信息失败：')){	 
				return ;
			}
			
			var msg = data.msg;
			alert("成功设置表信息");
			
			//	tableInfo.showTips("提示",'成功刷新代理信息');														   
		}).fail(function(){
			tableInfo.showTips("提示",'连接服务器失败');
		});
	},
	showTips:function(title,msg)
	{
		$.messager.show({
				title:title,
				msg:msg,
				timeout:3000,
				showType:'slide'
			});
	},
	

}